﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.loading.loaders {
    import flash.utils.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.loading.interfaces.*;
    import org.osflash.signals.*;

    public class LoaderGroup implements ILoaderGroup {

        private var _result:ResultVO;
        private var _results:Dictionary;
        private var _loaders:Dictionary;
        private var _onResult:Function;
        private var _onError:Function;
        private var _id:String;
        private var _isActive:Boolean;// = false
        private var _totalSize:int;// = 0
        private var _completed:Signal;
        private var _progressing:Signal;

        public function LoaderGroup(_arg1:String, _arg2:Function){
            this._result = new ResultVO();
            this._results = new Dictionary();
            this._loaders = new Dictionary();
            this._completed = new Signal(String);
            this._progressing = new Signal(String, int, int);
            super();
            this._id = _arg1;
            this._onResult = _arg2;
        }
        public function get id():String{
            return (this._id);
        }
        public function get completed():Signal{
            return (this._completed);
        }
        public function get progressing():Signal{
            return (this._progressing);
        }
        public function setActive():void{
            this._isActive = true;
            this.checkLoadingComplete();
        }
        public function add(_arg1:IRequestLoader):void{
            if (!this._loaders[_arg1.path]){
                this._totalSize = (this._totalSize + _arg1.totalSize);
                this._loaders[_arg1.path] = _arg1;
                _arg1.loaded.addOnce(this.handleLoaded);
                _arg1.progressing.add(this.handleProgress);
            };
        }
        public function set onGroupError(_arg1:Function):void{
            this._onError = _arg1;
        }
        private function handleProgress():void{
            var _local2:IRequestLoader;
            var _local1:int;
            for each (_local2 in this._loaders) {
                _local1 = (_local1 + _local2.currentSize);
            };
            this.progressing.dispatch(this._id, _local1, this._totalSize);
        }
        protected function handleLoaded(_arg1:String, _arg2:ResultVO):void{
            this._results[_arg1] = _arg2.data;
            if (!_arg2.success){
                this._result.success = false;
                this._onError(this._id, _arg2);
                this.completeLoading();
            } else {
                this.checkLoadingComplete();
            };
        }
        private function checkLoadingComplete():void{
            if (((this._isActive) && (this.loadersFinished))){
                this._loaders = null;
                this.completeLoading();
                if (this._onResult != null){
                    this._result.data = this._results;
                    this._onResult(this._result);
                };
            };
        }
        private function completeLoading():void{
            this.completed.dispatch(this._id);
            this.progressing.removeAll();
        }
        private function get loadersFinished():Boolean{
            var _local1:IRequestLoader;
            for each (_local1 in this._loaders) {
                if (!_local1.isFinished){
                    return (false);
                };
            };
            return (true);
        }

    }
}//package net.wooga.pets.loading.loaders 
